package cn.lijigou.shopping;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

import com.umeng.api.sns.UMSnsService;

import cn.lijigou.shopping.domain.CityTag;
import cn.lijigou.shopping.domain.MResult;
import cn.lijigou.shopping.service.LocationMapBar;
import cn.lijigou.shopping.service.MSystem;
import cn.lijigou.shopping.util.GetSingleThreadPool;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SettingsActivity extends MActivity implements OnClickListener{
	public static final int IS_VERSION = 0;
	public static final int NOT_VERSION = 1;
	
	private LinearLayout layout_user_info = null;
	private LinearLayout layout_propose = null;
	private LinearLayout layout_version = null;
	private LinearLayout layout_about = null;
	private LinearLayout layout_apps = null;
	private LinearLayout layout_change_city = null;
	private LinearLayout layout_FAQ = null;
	private CheckBox checkbox_push = null;
	private TextView userName = null;
	private MainTabActivity parentView;
	int x,lastX,init;
	private TextView mTitle_btn_right = null;
	private LinearLayout titleRightView = null;
	private TextView mTitle_btn_left = null;
	private TextView mTitle_btn_center = null;
	private ProgressDialog mProgress;
	private String  versionData;
	private ExecutorService executorService = GetSingleThreadPool.getInstance();
	
	public Handler mHander = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(msg.what==SettingsActivity.IS_VERSION){
				hideProgress();
				updateVersion();
			}
			if(msg.what==SettingsActivity.NOT_VERSION){
				hideProgress();
				Toast.makeText(SettingsActivity.this, R.string.notVersion, 1).show();
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.settings);
		super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.push_in,R.anim.push_out);
		setViews();
		setData();
		setOnClickListeners();
	
		SharedPreferences sp = SettingsActivity.this.getSharedPreferences("user", Context.MODE_PRIVATE);
		Boolean mSwitch = sp.getBoolean("push_switch", true);
		checkbox_push.setChecked(mSwitch);
	}
	
	@Override
	protected void onRestart() {
		setData();
		super.onRestart();
	}
	private void  setViews(){
		layout_user_info = (LinearLayout) findViewById(R.id.layout_user_info);
		layout_propose = (LinearLayout) findViewById(R.id.layout_propose);
		layout_version = (LinearLayout)findViewById(R.id.layout_version);
		layout_about = (LinearLayout)findViewById(R.id.layout_about_us);
		layout_apps = (LinearLayout)findViewById(R.id.layout_apps);
		layout_change_city = (LinearLayout)findViewById(R.id.layout_change_city);
		layout_FAQ = (LinearLayout)findViewById(R.id.layout_FAQ);
		checkbox_push = (CheckBox)findViewById(R.id.checkbox_push);

		userName = (TextView)findViewById(R.id.user_name);
		parentView=(MainTabActivity)SettingsActivity.this.getParent();
		mTitle_btn_left = (TextView) findViewById(R.id.title_btn_left);
		mTitle_btn_right = (TextView) findViewById(R.id.title_btn_right);
		mTitle_btn_right.setText(R.string.register);
		titleRightView = (LinearLayout) findViewById(R.id.titleRightView);
		mTitle_btn_center = (TextView)findViewById(R.id.text_center);
		mTitle_btn_center.setText(R.string.settings);
		mTitle_btn_left.setVisibility(View.GONE);
		
		findViewById(R.id.titleLeftView).setVisibility(View.GONE);
	}
	private void  setData(){
		MSystem.isLogin(this,0);
		if(!"".equals(MSystem.nickname)){
			userName.setText(getResources().getString(R.string.my_account)+MSystem.nickname);
			mTitle_btn_right.setText("退出登录");
		}else if(!"".equals(MSystem.uid)){
			userName.setText(getResources().getString(R.string.my_account)+MSystem.uid);
		}else{
			userName.setText(R.string.You_are_not_logged_in);
		}
	}
	private void  setOnClickListeners(){
		layout_user_info.setOnClickListener(this);
		layout_propose.setOnClickListener(this);
		layout_version.setOnClickListener(this);
		layout_about.setOnClickListener(this);
		layout_apps.setOnClickListener(this);
		layout_change_city.setOnClickListener(this);
		layout_FAQ.setOnClickListener(this);
		
		titleRightView.setOnClickListener(this);
		mTitle_btn_left.setOnClickListener(this);
		checkbox_push.setOnClickListener(this);
	}

	public void onClick(View v) {
		MSystem.pv += 1;
		switch (v.getId()) {
		case R.id.title_btn_left:
			this.finish();
			break;
		case R.id.titleRightView:
			if(mTitle_btn_right.getText().equals("退出登录")){
				SharedPreferences sp = SettingsActivity.this.getSharedPreferences("user", Context.MODE_PRIVATE);
				Editor edit = sp.edit();
				edit.putString("uid","");
				edit.putString("session","");
				edit.putString("nickname","");
				edit.putString("isTmp","0");
				edit.commit();
				mTitle_btn_right.setText("注册");
				userName.setText(R.string.You_are_not_logged_in);
				//注销微博登入
			    UMSnsService.writeOffAccount(this, UMSnsService.SHARE_TO.SINA);
			    UMSnsService.writeOffAccount(this, UMSnsService.SHARE_TO.TENC);
			    UMSnsService.writeOffAccount(this, UMSnsService.SHARE_TO.RENR);
			    Toast.makeText(this, "退出账号成功", 1).show();
			}else{
				Intent mIntent = new Intent(this, RegisterActivity.class);
				this.startActivity(mIntent);
			}
			
			break;
		case R.id.layout_user_info:
			Intent intent_userinfo = new Intent(this, LoginActivity.class);
			this.startActivity(intent_userinfo);
			break;
		case R.id.layout_propose:
			Intent intent_propose = new Intent(this, ProposeActivity.class);
			this.startActivity(intent_propose);
			break;
		case R.id.layout_about_us:
			Intent intent_about = new Intent(this, AboutUsActivity.class);
			this.startActivity(intent_about);
			break;
		case R.id.layout_version:
			executorService.execute(new Thread() {
				public void run() {
					Log.d("threadpool", "check_version----run---" + Thread.currentThread().getName());
					showProgress();
					MResult mResult = MSystem.checkVersion();
					if (mResult != null) {
						if (mResult.isSuccess()) {
							versionData = mResult.data;
							mHander.sendEmptyMessage(SettingsActivity.IS_VERSION);
						} else {
							mHander.sendEmptyMessage(SettingsActivity.NOT_VERSION);
						}
					}

				}
			});
			break;
		case R.id.layout_apps:
			Intent intent_apps = new Intent(this, AppsActivity.class);
			this.startActivity(intent_apps);
			break;
		case R.id.layout_change_city:
			Intent intent_change = new Intent(this, ChangeCityActivity.class);
			intent_change.putExtra("from", "setting");
			this.startActivity(intent_change);
		 
			break;
		case R.id.layout_FAQ:
			Intent intent_FAQ = new Intent(this, FAQActivity.class);
			this.startActivity(intent_FAQ);
			break;
		case R.id.checkbox_push:
			SharedPreferences sp = SettingsActivity.this.getSharedPreferences("user", Context.MODE_PRIVATE);
			Editor edit = sp.edit();
			if(checkbox_push.isChecked()){
				edit.putBoolean("push_switch", true);
			}else{
				edit.putBoolean("push_switch", false);
			}
			edit.commit();
			break;
		}
	}
	
	public void updateVersion(){
		new AlertDialog.Builder(this)
		.setIcon(R.drawable.icon)
		.setTitle(R.string.upgrade_tips)
		.setCancelable(false)
		.setMessage(versionData)
		.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				Uri uri = Uri.parse("http://www.lookoo.cn/tuangou.apk");
				if(uri == null) {
					return ;
				}
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
		})
		.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		})
		.create().show();
	}
	  @Override
	    public boolean onKeyDown(int keyCode, KeyEvent event) {
	    	if (keyCode == KeyEvent.KEYCODE_BACK) {
	  			Animation animation=AnimationUtils.loadAnimation(SettingsActivity.this,R.anim.home_push_rightout);
    			parentView.mHost.getCurrentView().startAnimation(animation);
        		parentView.mHost.setCurrentTab(0);	
    			parentView.main_tab_home.setChecked(true);
			    Animation animations=AnimationUtils.loadAnimation(SettingsActivity.this,R.anim.home_push_rightin);
			    parentView.mHost.getCurrentView().startAnimation(animations);
	    		return true;
		    }else{
		    	return super.onKeyDown(keyCode, event);
		    }
	    }
	    /**
		 * 显示等待框
		 * 
		 * @param title
		 * @param message
		 */
		protected void showProgress() {
			this.runOnUiThread(new Runnable() {
				

				public void run() {	
					mProgress = new ProgressDialog(SettingsActivity.this);
					mProgress.requestWindowFeature(Window.FEATURE_NO_TITLE);
					mProgress.setMessage("正在检查新版本，请稍等...");
					mProgress.show();
				}
			});
		}

		/**
		 * 取消等待框
		 */
		protected void hideProgress() {
			
			this.runOnUiThread(new Runnable() {
				public void run() {
					if (mProgress != null) {
						mProgress.dismiss();
					}
				}
			});
		}
}
